package com.cd.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;


import com.cd.bean.Reply;

import com.cd.service.reply.ReplyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 留言回复
 *@Author zhk
 *
 */
@RequestMapping("/reply")
@Controller
public class ReplyController {

	@Autowired
	ReplyService replyService;
	
	@RequestMapping("/save")
	public String save(@RequestParam(value="REPLY_ID")String id,
			@RequestParam(value="REPLY_CONTENT")String replycontent) throws Exception {
		Reply reply= new Reply();
		reply.setREPLY_ID(id);
		reply.setREPLY_CONTENT(replycontent);
		replyService.save(reply);
		return  "redirect:/msg/list.do";
	}

	
	/**
	 * 查询（带分页）
	 * @param pn
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/list")
	public ModelAndView list(@RequestParam(value = "pn",defaultValue="1")Integer pn) throws Exception {
		ModelAndView mv = new ModelAndView();
		PageHelper.startPage(pn, 10);//开始分页
	    List<Reply> list = replyService.listAll();
		PageInfo page = new PageInfo(list,5);//设置分页信息
		mv.addObject("pageInfo", page);
		mv.setViewName("admin/wsly/ly_list");
		return mv;
	}
	/**
	 * 删除
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/delete")
	public ModelAndView delete(@RequestParam(value="id")Integer id) throws Exception{
		Reply reply =replyService.findById(id);
		ModelAndView mv = new ModelAndView();
		replyService.delete(id);
		mv.setViewName("redirect:list.do");
		return mv;
	}
	

	/**
	 * 去编辑界面
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/goEdit")
	public ModelAndView goEdit(@RequestParam(value="id")Integer id) throws Exception {
		ModelAndView mv = new ModelAndView();
		Reply  reply =replyService.findById(id);
		mv.addObject("reply", reply);
		mv.setViewName("admin/wsly/ly_edit");
		return mv;
	}
    /**
     * 编辑
     * @param id
     * @param replycontent
     * @return
     * @throws Exception
     */
	@RequestMapping("/edit")
	public ModelAndView eidt(@RequestParam(value="REPLY_ID")Integer id,
			@RequestParam(value="REPLY_CONTENT")String replycontent) throws Exception {
			ModelAndView mv = new ModelAndView();
			Reply reply= replyService.findById(id);
			reply.setREPLY_CONTENT(replycontent);
			replyService.edit(reply);
			mv.setViewName("redirect:list.do");
		return mv;
	}
	
	@RequestMapping("/findById")
	public ModelAndView findReplyById(@RequestParam(value = "id") Integer id) throws Exception{
		ModelAndView mv=new ModelAndView();
		Reply reply=replyService.findById(id);
	    mv.setViewName(" ");
		return mv;
		
	}
	/**
	 * 
	 * 批量删除
	 * @param request
	 * @param response
	 */
	@RequestMapping("/del")  
	public void batchDeletes(HttpServletRequest request, HttpServletResponse response) {  
	    String items = request.getParameter("delitems");
	    System.out.println(items);  
	    String[] strs = items.split(",");  
	  
	    for (int i = 0; i < strs.length; i++) {  
	        try {  
	            int a = Integer.parseInt(strs[i]);  
	            replyService.delete(a);  
	        } catch (Exception e) {  
	        }  
	    }  
	} 
	
}
